package at.com.vn.chess.network;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class ClientUtil {

	HttpMessage httpMessage;
	URL url;

	public ClientUtil(URL contx, String servletName) {
		try {
			url = new URL(contx, servletName);
			httpMessage = new HttpMessage(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public void sendBroadcastMessage(String xmlMessage) {
		try {
			Properties props = new Properties();
			props.put("broadcast", xmlMessage);

			httpMessage.sendPostMessage(props);
		} catch (SocketException e) {
			// Can't connect to host, report it and abandon the broadcast
			System.out.println( "Can't connect to host: " + e.getMessage());
		} catch (FileNotFoundException e) {
			// Servlet doesn't exist, report it and abandon the broadcast
			System.out.println( "sendBroadcastMessage-Resource not found: " + e.getMessage());
		} catch (Exception e) {
			// Some other problem, report it and abandon the broadcast
			System.out.println( "General exception: " + e.getClass().getName()
					+ ": " + e.getMessage());
		}
	}

	public String receiverMessage() {
		String nextMessage = null;
		while (nextMessage == null) {
			try {
				InputStream in = httpMessage.sendGetMessage();
				
				BufferedReader d = new BufferedReader(new InputStreamReader(in));

				nextMessage = d.readLine();

			} catch (SocketException e) {
				// Can't connect to host, report it and wait before trying again
				System.out.println( "Can't connect to host. Dang co gang de ket noi lai " );
				try {
					Thread.sleep(5000);
				} catch (InterruptedException ignored) {
					System.out.println( "Du doan: loi lien quan den thread" );
				}
			} catch (FileNotFoundException e) {
				// Servlet doesn't exist, report it and wait before trying again
				System.out.println( "receiverMessage-Resource not found: " + e.getMessage());
				try {
					Thread.sleep(5000);
				} catch (InterruptedException ignored) {
					System.out.println( "Du doan: loi lien quan den thread" );
				}
			} catch (Exception e) {
				// Some other problem, report it and wait before trying again
				System.out.println( "General exception: "
						+ e.getClass().getName() + ": " + e.getMessage());
				try {
					Thread.sleep(1000);
				} catch (InterruptedException ignored) {
					System.out.println( "Du doan: loi lien quan den thread" );
				}
			}
		}
		return nextMessage + "\n";
	}

	/**
	 * Get a connection to the servlet.
	 */
	private URLConnection getServletConnection(URL urlServlet) throws IOException {

		// Connection
		URLConnection con = urlServlet.openConnection();

		// konfigurieren
		con.setDoInput(true);
		con.setDoOutput(true);
		con.setUseCaches(false);
		con.setRequestProperty("Content-Type",
				"application/x-java-serialized-object");

		// und
		return con;
	}

	/**
	 * Send the inputField data to the servlet and show the result in the
	 * outputField.
	 */
	public String doPrivateData(String msgXML) {
		try {
			// send data to the servlet
			URLConnection servletConnection = getServletConnection(url);
			OutputStream outstream = servletConnection.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outstream);
			oos.writeObject(msgXML);
			oos.flush();
			oos.close();

			// receive result from servlet
			InputStream instr = servletConnection.getInputStream();
			ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
			String result = (String) inputFromServlet.readObject();
			inputFromServlet.close();
			instr.close();

			// show result
			return result;

		} catch (Exception ex) {

		}
		return "";
	}
}